<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Session;

class Surey extends Base
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $scoreId = Db::name('people')->where('id',session('user_id'))->value('section_id');
        $hospitalId = Db::name('section')->where('id',$scoreId)->value('hospital_id');
        $section = Db::name('section')->where('hospital_id',$hospitalId)->field('id,name')->select();
        $name = Db::name('section')->where('id',$hospitalId)->value('name');
        $id = Db::name('section')->where('id',$hospitalId)->value('id');
        $this->assign(['section'=>json_encode($section),'name'=>$name,'id'=>$id]);
        return $this->view->fetch();
    }
    public function sureyAdd(){
        $params = $this->request->post();
        $arr = array(
            'name'  => $params['name'],
            'sex'   => $params['sex'],
            'phone' => $params['phone'],
            'address'=> $params['address']
        );
        Db::name('people')->where('id',session('user_id'))->update($arr);
        $newArr = array(
            'people_id' => session('user_id'),
            'section_id'=> $params['sectionId'],
            'type'      => 2,
            'createtime'=> time()
        );
        $res = Db::name('survey')->insert($newArr);
        $survey_id = Db::name('survey')->getLastInsID();
        return $res?$survey_id:0;
    }
    public function answer(){
        $id = $this->request->get('id');
        $scoreId = Db::name('people')->where('id',session('user_id'))->value('section_id');
        $hospitalId = Db::name('section')->where('id',$scoreId)->value('hospital_id');
        $question = Db::name('question')->where('id',3)->value('id');
        $questions = Db::name('questions')->where('question_id',$question)->select();

        foreach ($questions as $k=>$v){
            $questions[$k]['option'] = explode('/',$v['option']);
            if($v['status'] == 1 || $v['status'] == 2){
                array_push($questions[$k]['option'],'其他');
            }
        }
        $count = count($questions)-1;
        $this->assign(['id'=>$id,'res'=>$questions,'count'=>$count]);
        return $this->view->fetch();
    }
    public function answerAdd(){
        $params = $this->request->post();
        $surverId = $this->request->get('id');
        foreach ($params as $k=>$v){
            $answer='';
            if(isset($v['answer'])){
                $answer .= $v['answer'];
            }
            if(isset($v['qita'])){
                $answer .= '/'.$v['qita'];
            }
            if(isset($v['qts'])){
                $answer .= implode('/',$v['qts']);
            }
            $arr = array(
                'surver_id' => $surverId,
                'questions_id'  => $v['id'],
                'answer'    => $answer
            );
            Db::name('answer')->insert($arr);
        }
        $res = Db::name('survey')->where('id',$surverId)->update(['type'=>1]);
        Db::name('question')->where('id',3)->setInc('count');
        return $res?200:400;
    }
    public function over(){
        return $this->view->fetch();
    }
}
